<?php
$this->useClass("Action_Community_FeedHandler");
class Action_Community_ShowFeedRss extends Action_Community_FeedHandler
{
	public function getFeedModel() {
		$this->rh->UseClass("AggregatedModel_Feed_FeedInterface");
		$this->rh->UseClass("AggregatedModel_Feed_ReaderItems");

		return new AggregatedModel_Feed_ReaderItems();
	}

	protected function init() {
		parent::init();
		$this->m_tdi = $this->ctx->mm->getModel("Tdi", "m");
		$tdi_oids = array();
		foreach ($this->aa->acl_data['tdi_list'] as $tdi) $tdi_oids[] = $tdi['id_object'];
		$this->tdi_types = $this->m_tdi->getSections($tdi_oids);

		$this->rh->UseClass("RL_URLGenerator");
	}

	public function process ()
	{
		$aa = &$this->aa;

		$aa->checkDeletions(array($this, "shutdown") );
		$this->model->setModelParams("page_citems", 20);
		$this->model->doStuff();

		$view = $this->rh->views->findByName($this->params['view']);
		$view->data = $aa->template_data;
		$view->data['tdi'] = $this->aa->acl_data['tdi']->data;
		$items = $this->model->getItems();
		
		if ($items) {
			foreach($items as $k=>$v) {
				$date = clone $v['post']['ctime'];
				$items[$k]['type'] = $this->tdi_types[ $v['post']['tdi_oid'] ];
				$items[$k]['date'] = $date;
				$items[$k]['post_url'] = $this->hrefPost($v);
			}
			$view->getMeta()->setLastModified($items[0]['date']->mktime());
			$view->data['items'] = $items;
		} else $view->data['message'] = "В этой ленте нет сообщений.";

		$this->rh->response->setContent( $view );
	} // end of function process

	public function hrefPost ($item)
	{
		switch ($item['doctype']) {
		case 'quest':
			$ug = new RL_URLGenerator($this->rh->router->getRule("show_quest")); //
			$ug->setRouter($this->rh->router);
			$ug->setParams(array(
				"community_url"=>$this->params['community_url'],
				"id_quest"=>$item['post']['oid'],
				)
			);
			break;
		default:
			$ug = new RL_URLGenerator($this->rh->router->getRule("showitem")); //
			$ug->setRouter($this->rh->router);
			$ug->setParams(array(
				"community_url"=>$this->params['community_url'],
				"id_item"=>$item['post']['oid'],
				)
			);
			break;
		}
		return $ug->generateUrl();
	} // end of method hrefPost

}
